package com.vonchange.water.parser.worker;

import com.vonchange.water.domain.main.LineModel;
import com.vonchange.water.domain.main.NodeModel;
import com.vonchange.water.parser.antlr.MermaidFlowLexer;
import com.vonchange.water.parser.antlr.MermaidFlowParser;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.Map;

public class Test {

        public static void main(String[] args) throws Exception {
            // N1((开始)) -- 开始条件的 --> N2[起草节点]
            String input = " A  -->|到b| B{bbbb} -- \"text 22334\" --> C ";
            System.out.println(input);
            // 创建一个词法分析器，用于将输入转换为标记
            MermaidFlowLexer lexer = new MermaidFlowLexer(CharStreams.fromString(input));
            FlowRowVisitor eval = null;
            try{
                // 创建一个标记流，用于将标记传递给解析
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                MermaidFlowParser parser = new MermaidFlowParser(tokens);
                // 调用解析器的parse方法，生成AST
                parser.setErrorHandler(new BailErrorStrategy());
                ParseTree tree = parser.row();

                // 创建一个AST遍历器，用于计算表达式的值
                 eval = new FlowRowVisitor();
                 eval.visit(tree);
            }catch (Exception e){
                e.printStackTrace();
            }

            Map<String, NodeModel> nodeModelMap = eval.getNodeMap();
            for (LineModel lineModel : eval.getLineList()) {
                nodeModelMap.get(lineModel.getFromId()).getOutLines().add(lineModel);
            }
            System.out.println(eval.getNodeMap().toString());

    }

}
